﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace WomanMotion.WebApi.Infrastructure.YitId
{
    /// <summary>
    /// 雪花ID生成器
    /// </summary>
    public class YitIdHelper
    {
        private static IIdGenerator _IdGenInstance = null;
        static YitIdHelper()
        {
            var options = new IdGeneratorOptions(8);
            options.WorkerIdBitLength = 10;
            options.SeqBitLength = 6;
            // 保存参数（务必调用，否则参数设置不生效）：
            YitIdHelper.SetIdGenerator(options);
        }
        /// <summary>
        /// 设置参数，建议程序初始化时执行一次
        /// </summary>
        /// <param name="options"></param>
        public static void SetIdGenerator(IdGeneratorOptions options)
        {
            _IdGenInstance = new DefaultIdGenerator(options);
        }
        /// <summary>
        /// 生成新的Id
        /// 调用本方法前，请确保调用了 SetIdGenerator 方法做初始化。
        /// </summary>
        /// <returns></returns>
        public static long NextId()
        {
            if (_IdGenInstance == null) throw new ArgumentException("Please initialize Yitter.IdGeneratorOptions first.");
            return _IdGenInstance.NewLong();
        }
    }
}
